import axios from 'axios'
import config from './config.js'
if (process.env.NODE_ENV == 'development') {
	axios.defaults.baseURL = '';
}
else if (process.env.NODE_ENV == 'debug') {
	axios.defaults.baseURL = config.baseUrl;
}
else if (process.env.NODE_ENV == 'production') {
	axios.defaults.baseURL = config.baseUrlPro;
}
// 请求超时时间
axios.defaults.timeout = 3600 * 1000;
let baseURL=config.baseUrl
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';
import useLoadingCountStore from "~/stores/loadingCount"; //引入仓库

const showLoading = (params) => {
	if (params.noloading == undefined || !params.noloading) {
		const loadingCountStore = useLoadingCountStore();
		loadingCountStore.add();
	}
}
const closeLoading = (params) => {
	if (params.noloading == undefined || !params.noloading) {
		const loadingCountStore = useLoadingCountStore();
		loadingCountStore.reduce();
	}
}
import { message } from 'ant-design-vue';
import { useRouter } from 'vue-router';
import Cookie from '~/components/cookie.vue';
import Cookies from 'js-cookie';

const router = useRouter();
let timer = null;
let resolveFuc = (res, config, resolve, reject) => {
	debugger
	closeLoading(config);
	switch(res.status) {
		case 201: 
		case 200: {
			resolve(res.data);
			break;
		}
		case 401: {
			router.replace({
				name: 'Login'
			})
			message.error('请先登录！');
			reject(res.data);		
			break;	  
		}
		default: {
			message.error('请求错误！');
			reject(res.data);		
			break;	  
		}
	}
}
export function get(url, params, config, headers) {
	clearTimeout(timer);
	timer = null;
	url=baseURL+url
	showLoading(config);
	return new Promise((resolve, reject) => {
		axios.get(url, {
			headers: headers,
			params: params
		}).then(res => {
			resolveFuc(res, config, resolve, reject);
		}).catch(err => {
			if(err.response.status==401){
				Cookies.set('user-tk','')
				Cookies.set('user-data','')
				message.error({
					content: () => '请先登录！',
					class: 'custom-class',
					style: {
					  marginTop: '20vh',
					},
				  });
			}
			if(err.response.status==403){
				message.error({
					content: () => '没有操作权限！',
					class: 'custom-class',
					style: {
					  marginTop: '20vh',
					},
				  });
			}
			closeLoading(config);
			reject(err.data)
		})
	})
}
export function post(url, params, config, headers) {
	timer = null;
	showLoading(config);
	url=baseURL+url
	return new Promise((resolve, reject) => {
		axios.post(url, params, {
			headers: headers
		})
			.then(res => {
				resolveFuc(res, config, resolve, reject);
			}).catch(err => {
				if(err.response.status==401){
					Cookies.set('user-tk','')
					Cookies.set('user-data','')
					message.error({
						content: () => '请先登录！',
						class: 'custom-class',
						style: {
						  marginTop: '20vh',
						},
					  });
				}
				if(err.response.status==403){
					message.error({
						content: () => '没有操作权限！',
						class: 'custom-class',
						style: {
						  marginTop: '20vh',
						},
					  });
				}
				closeLoading(config);
				reject(err.data)
			})
	});
}
export function patchFuc(url, params, config, headers) {
	timer = null;
	showLoading(config);
	url=baseURL+url
	return new Promise((resolve, reject) => {
		axios.patch(url, params, {
			headers: headers
		})
			.then(res => {
				resolveFuc(res, config, resolve, reject);
			}).catch(err => {
				if(err.response.status==401){
					Cookies.set('user-tk','')
					Cookies.set('user-data','')
					message.error({
						content: () => '请先登录！',
						class: 'custom-class',
						style: {
						  marginTop: '20vh',
						},
					  });
				}
				if(err.response.status==403){
					message.error({
						content: () => '没有操作权限！',
						class: 'custom-class',
						style: {
						  marginTop: '20vh',
						},
					  });
				}
				closeLoading(config);
				reject(err.data)
			})
	});
}
export function deleteFuc(url, params, config, headers) {
	debugger
	timer = null;
	showLoading(config);
	url=baseURL+url
	return new Promise((resolve, reject) => {
		axios.delete(url, {
			headers: headers
		})
			.then(res => {
				resolveFuc(res, config, resolve, reject);
			}).catch(err => {
				if(err.response.status==401){
					Cookies.set('user-tk','')
					Cookies.set('user-data','')
					message.error({
						content: () => '请先登录！',
						class: 'custom-class',
						style: {
						  marginTop: '20vh',
						},
					  });
				}
				if(err.response.status==403){
					message.error({
						content: () => '没有操作权限！',
						class: 'custom-class',
						style: {
						  marginTop: '20vh',
						},
					  });
				}
				closeLoading(config);
				reject(err.data)
			})
	});
}
